<template>
  <div class="all">
    

    <div class="swiper-container swiper-no-swiping" ref="swiper">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="(imgUrl, id) in list" :key="imgUrl.id">
          <img :src="imgUrl.imgUrl" @click="goSearch(imgUrl.title)" alt="" />
        </div>
      </div>
    </div>

    <div class="swiper-pagination"></div>

    <div
      class="swiper-button-prev"
      
    ></div>
    <div
      class="swiper-button-next"
      
    ></div>
  </div>
</template>

<script>
import Swiper from "swiper";
import { mapState } from "vuex";
export default {
  name: "swiper",
  data() {
    return {
    
    };
  },
  mounted() {
    this.$store.dispatch("get_banner");
  },
  methods: {
    goSearch(index){
      //提前准备数据(解决：在搜索页面进行的第二次搜索)
      this.$store.dispatch("search_song",index)
      this.$router.push({name:"search",query:{text:index}})
       
    }
  },
  computed: {
    ...mapState({
      list: (state) => state.home.banner,
    }),
  },
  watch: {
    list: {
      // immediate: true,
      handler(newValue, oldValue) {
        this.$nextTick(() => {
          var mySwiper = new Swiper(
            document.querySelector(".swiper-container"),
            {
              loop: true,
              pagination: {
                el: ".swiper-pagination",
                clickable: true,
              },
              navigation: {
                nextEl: ".swiper-button-next",
                prevEl: ".swiper-button-prev",
              },
              autoplay: {
                delay: 5000, //1秒切换一次
              },
              speed:500,
            }
          );
        });
      },
    },
    
  },
};
</script>

<style lang="less">
.all {
  position: relative;
  width: 700px;
  height: 250px;
  overflow: hidden;
  .swiper-container {
    width: 600px;
    line-height: 250px;
    margin-top: 27px;
    border-radius: 5px;
    img {
      height: 196px;
      border-radius: 5px;
    }
  }


  .swiper-pagination {
    position: absolute;
    bottom: 0;
    .swiper-pagination-bullet {
      margin-left: 5px;
      margin-right: 5px;
    }
  }

  .swiper-button-prev::after {
    text-align: center;
    font-size: 20px;
    color: #333;
    opacity: 0.7;
  }
  .swiper-button-next::after {
    text-align: center;
    width: 100%;
    font-size: 20px;
    color: #333;
    opacity: 0.7;
  }
}
</style>